package com.example.javamaday.leetcode;

import com.example.javamaday.leetcode.base.TreeNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

// 二叉树的中序遍历
public class LeetCodeTest0094 {

    public static void main(String[] args) {

        TreeNode root = new TreeNode(1);
        root.right = new TreeNode(2);
        root.right.left = new TreeNode(3);
        List<Integer> list = new LeetCodeTest0094().inorderTraversal(root);
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }

    private ArrayList<Integer> result = new ArrayList<>();

    public List<Integer> inorderTraversal(TreeNode root) {
        if (root == null) {
            return result;
        }
        Stack<TreeNode> treeNodeStack = new Stack<>();
        while(root != null || !treeNodeStack.isEmpty()) {
            if (root != null) {
                treeNodeStack.push(root);
                root = root.left;
            } else {
                root = treeNodeStack.pop();
                result.add(root.val);
                root = root.right;
            }
        }
        return result;
    }


//    public List<Integer> inorderTraversal(TreeNode root) {
//        if (root == null) {
//            return result;
//        }
//        if (root.left != null) {
//            inorderTraversal(root.left);
//        }
//        result.add(root.val);
//        if (root.right != null) {
//            inorderTraversal(root.right);
//        }
//        return result;
//    }
}
